package com.ruoyi.web.controller.mass;

import java.util.List;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.ApplyDto;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysMessage;
import com.ruoyi.system.service.ISysMessageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 系统内部消息通知Controller
 *
 * @author ruoyi
 * @date 2024-06-09
 */
@RestController
@RequestMapping("/system/message")
public class SysMessageController extends BaseController
{
    @Autowired
    private ISysMessageService sysMessageService;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询系统内部消息通知列表
     */
    @SaCheckPermission("system:message:list")
    @GetMapping("/list")
    public TableDataInfo list(SysMessage sysMessage)
    {
        startPage();
        List<SysMessage> list = sysMessageService.selectSysMessageList(sysMessage);
        return getDataTable(list);
    }

    /**
     * 导出系统内部消息通知列表
     */
    @SaCheckPermission("system:message:export")
    @Log(title = "系统内部消息通知", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysMessage sysMessage)
    {
        List<SysMessage> list = sysMessageService.selectSysMessageList(sysMessage);
        ExcelUtil<SysMessage> util = new ExcelUtil<SysMessage>(SysMessage.class);
        return util.exportExcel(list, "系统内部消息通知数据");
    }

    /**
     * 获取系统内部消息通知详细信息
     */
    @SaCheckPermission("system:message:query")
    @GetMapping(value = "/{messageId}")
    public AjaxResult getInfo(@PathVariable("messageId") Long messageId)
    {
        return AjaxResult.success(sysMessageService.selectSysMessageById(messageId));
    }

    /**
     * 申请加入社团
     */

    @PostMapping(value = "/applyJoinMass")
    public AjaxResult applyJoinMass(@RequestBody ApplyDto applyDto)
    {
        //申请人ID
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        //给社团管理员审批
        Long adminId = deptService.selectDeptAdminIdByMassId(applyDto.getDeptId());
        applyDto.setReceiveId(adminId);
        applyDto.setSendId(userId);

        return AjaxResult.success(sysMessageService.sendMessageToAdmin(applyDto));
    }
    /**
     * 审批社团申请
     */
    @SaCheckPermission("system:message:approve")
    @PostMapping(value = "/approve")
    public AjaxResult approve(@RequestBody SysMessage sysMessage)
    {
        return AjaxResult.success(sysMessageService.approveJoinMass(sysMessage));
    }

    /**
     * 新增系统内部消息通知
     */
    @SaCheckPermission("system:message:add")
    @Log(title = "系统内部消息通知", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMessage sysMessage)
    {
        return toAjax(sysMessageService.insertSysMessage(sysMessage));
    }

    /**
     * 修改系统内部消息通知
     */
    @SaCheckPermission("system:message:edit")
    @Log(title = "系统内部消息通知", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMessage sysMessage)
    {
        return toAjax(sysMessageService.updateSysMessage(sysMessage));
    }

    /**
     * 删除系统内部消息通知
     */
    @SaCheckPermission("system:message:remove")
    @Log(title = "系统内部消息通知", businessType = BusinessType.DELETE)
    @DeleteMapping("/{messageIds}")
    public AjaxResult remove(@PathVariable Long[] messageIds)
    {
        return toAjax(sysMessageService.deleteSysMessageByIds(messageIds));
    }
}
